package cn.iocoder.yudao.module.system.api.message;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.api.message.dto.*;
import cn.iocoder.yudao.module.system.controller.admin.message.vo.MessageCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.message.vo.MessagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.message.vo.MessageUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.messageInfo.vo.MessageInfoCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.messageInfo.vo.MessageInfoUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.message.MessageConvert;
import cn.iocoder.yudao.module.system.convert.messageInfo.MessageInfoConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.message.MessageDO;
import cn.iocoder.yudao.module.system.dal.dataobject.messageInfo.MessageInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.messageType.MessageTypeDO;
import cn.iocoder.yudao.module.system.service.message.MessageService;
import cn.iocoder.yudao.module.system.service.messageInfo.MessageInfoService;
import cn.iocoder.yudao.module.system.service.messageType.MessageTypeService;
import jodd.bean.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.security.auth.message.MessageInfo;
import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MessageApiImpl implements MessageApi{

    @Resource
    private MessageService messageService;

    @Resource
    private MessageInfoService messageInfoService;

    @Resource
    private MessageTypeService messageTypeService;

    @Override
    @TenantIgnore
    public Long sendMessage(MessageCreateReqDTO messageCreateReqDTO) {
        MessageCreateReqVO createReqVO=new MessageCreateReqVO();
        BeanUtils.copyProperties(messageCreateReqDTO,createReqVO);
        return messageService.createMessage(createReqVO);
    }

    @Override
    @TenantIgnore
    public void updateMessage(MessageUpadateReqDTO messageUpadateReqDTO) {
        MessageUpdateReqVO messageUpdateReqVO=new MessageUpdateReqVO();
        BeanUtils.copyProperties(messageUpadateReqDTO,messageUpdateReqVO);
        messageService.updateMessage(messageUpdateReqVO);
    }

    @Override
    @TenantIgnore
    public MessageRespDTO getMessage(Long id) {
        //消息主体
        MessageDO messageDO= messageService.getMessage(id);
        MessageRespDTO messageRespDTO=new MessageRespDTO();
        BeanUtils.copyProperties(messageDO,messageRespDTO);
        //消息类型
        MessageTypeDO messageTypeDO= messageTypeService.getMessageType(messageRespDTO.getType());
        if(messageTypeDO==null){
            messageRespDTO.setTypeName("未知类型");
        }else {
            messageRespDTO.setTypeName(messageTypeDO.getName());
        }
        //消息内容
        List<MessageInfoRespDTO> messageInfoRespDTOList=this.getMessageInfoList(messageDO.getId());
        messageRespDTO.setMessageInfoRespDTOList(messageInfoRespDTOList);
        return messageRespDTO;
    }

    @Override
    @TenantIgnore
    public PageResult<MessageRespDTO> getMessagePage(MessagePageReqDTO messageCreateReqDTO) {
        MessagePageReqVO messagePageReqVO=new MessagePageReqVO();
        BeanUtils.copyProperties(messageCreateReqDTO,messagePageReqVO);
        List<MessageTypeDO> messageTypeList = messageTypeService.getMessageTypeList();
        List<Long> ids = messageTypeList.stream().map(m -> m.getId()).collect(Collectors.toList());
        messagePageReqVO.setTypeIds(ids);
        PageResult<MessageDO> messagePage = messageService.getMessagePage(messagePageReqVO);
        if(messagePage.getTotal()>0){
            List<MessageDO> list = messagePage.getList();
            for (MessageDO messageDO : list) {
                MessageTypeDO messageTypeDO = messageTypeList.stream().filter(m -> (m.getId()).equals(messageDO.getType())).findFirst().orElse(null);
                if(null!=messageTypeDO){
                    messageDO.setTypeName(messageTypeDO.getName());
                }
            }
        }
        return MessageConvert.INSTANCE.convertPage1(messagePage);
    }

    @Override
    @TenantIgnore
    public List<MessageInfoRespDTO> getMessageInfoList(Long messageId) {
        return MessageInfoConvert.INSTANCE.convertList1(messageInfoService.getMessageInfoList(messageId));
    }

    @Override
    public void changeMessageStatus(List<Long> ids,String status, Integer type) {
        if(type.equals(0)){
            if(status.equals("1")){
                status="1";
            }else {
                status="4";
            }
        }else if(type.equals(1)){
            if(status.equals("1")){
                status="3";
            }else {
                status="2";
            }
        }
       List<MessageDO> messageList=messageService.getMessageList(ids);
        for (MessageDO messageDO:messageList) {
            MessageUpdateReqVO updateReqVO=new MessageUpdateReqVO();
            BeanUtils.copyProperties(messageDO,updateReqVO);
            updateReqVO.setStatus(status);
            messageService.updateMessage(updateReqVO);

        }
    }

    @Override
    public void allchangeMessageStatus(String status, Integer type,Long studentId,Long userId) {
        if(type.equals(0)){
            if(status.equals("1")){
                status="1";
            }else {
                status="4";
            }
        }else if(type.equals(1)){
            if(status.equals("1")){
                status="3";
            }else {
                status="2";
            }
        }
        List<MessageDO> messageList=messageService.getMessageListByUserId(studentId,userId);
        for (MessageDO messageDO:messageList) {
            if(messageDO.getStatus().equals("2")||messageDO.getStatus().equals("4")||messageDO.getStatus().equals("5")) {
                MessageUpdateReqVO updateReqVO = new MessageUpdateReqVO();
                BeanUtils.copyProperties(messageDO, updateReqVO);
                updateReqVO.setStatus(status);
                messageService.updateMessage(updateReqVO);
            }
        }
    }

    @Override
    @TenantIgnore
    public void replyMessage(MessageInfoCreateReqDTO messageInfoCreateReqDTO) {
        MessageInfoCreateReqVO createReqVO=new MessageInfoCreateReqVO();
        BeanUtils.copyProperties(messageInfoCreateReqDTO,createReqVO);
        messageInfoService.createMessageInfo(createReqVO);
    }

    @Override
    public int noOpenMessageCount(Long studentId) {
        return messageService.noOpenMessageCount(studentId);
    }


}
